﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum
{
	public partial class PostPad : AlbumPage
	{
		private const int POST_MAX_WIDTH = 120;
		private const int POST_MAX_HEIGHT = 90;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			postPad.CancelButtonText = Resources.Strings.Cancel;
			postPad.ChooseLocationText = Resources.Strings.PostPad_ChooseLocation;
			postPad.SelectText = Resources.Strings.PostPad_SelectFile;
			postPad.SubmitButtonText = Resources.Strings.PostPad_PostIt;
			postPad.WaitText = Resources.Strings.My_UploadingPhoto;

			postPad.PhotoPosted += new OpenPhotoAlbum.Controls.PostPad.PostPadEventHandler(postPad_PhotoPosted);
		}

		protected void postPad_PhotoPosted(object sender, Controls.PostPadEventArgs e)
		{
			MemoryStream stream = new MemoryStream(e.Data, false);

			string canvasLocalAddress = HttpContext.Current.Server.MapPath(Links.PostPadCanvas);

			Bitmap canvas = new Bitmap(canvasLocalAddress);
			Bitmap target = new Bitmap(BusinessLogics.PhotoManager.Instance.ResizePhoto(e.Data, POST_MAX_WIDTH, POST_MAX_HEIGHT));

			Bitmap result = PostPadProcessor.Instance.PostOnCanvas(canvas, target, e.OffsetX, e.OffsetY, e.Angle);

			target.Dispose();
			canvas.Dispose();

			result.Save(canvasLocalAddress);

			result.Dispose();
			stream.Close();

			lock (PhotoPipe.postPadCanvasCache_LockSignal) { PhotoPipe.postPadCanvasCache = null; }
		}
	}
}
